package com.atguigu.gmall.activity.controller;

import com.atguigu.gmall.activity.service.SeckillGoodsService;
import com.atguigu.gmall.activity.util.CacheHelper;
import com.atguigu.gmall.activity.util.DateUtil;
import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.common.util.MD5;
import com.atguigu.gmall.model.activity.OrderRecode;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.atguigu.gmall.model.activity.UserRecode;
import com.atguigu.gmall.model.order.OrderDetail;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.user.UserAddress;
import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.user.client.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author mqx
 * @date 2020-8-18 11:14:03
 */
@RestController
@RequestMapping("/api/activity/seckill")
public class SeckillGoodsApiController {

    // 引入服务层对象
    @Autowired
    private SeckillGoodsService seckillGoodsService;

    @Autowired
    private RabbitService rabbitService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private OrderFeignClient orderFeignClient;

    // 查询所有数据
    @GetMapping("findAll")
    public Result findAll(){
        // 获取所有数据
        List<SeckillGoods> seckillGoodsList = seckillGoodsService.findAll();

        return Result.ok(seckillGoodsList);
    }

    // 根据skuId 获取秒杀对象数据
    @GetMapping("/getSeckillGoods/{skuId}")
    public Result getSeckillGoods(@PathVariable Long skuId){
        // 直接返回！
        SeckillGoods seckillGoods = seckillGoodsService.getSeckillGoods(skuId);
        return Result.ok(seckillGoods);
    }

    // 获取下单码： 为了防止用户直接跳过商品详情进入秒杀url！
    // 下单码：生成规则 将用户Id{userId},md5的一种加密作为下单码！
    @GetMapping("auth/getSeckillSkuIdStr/{skuId}")
    public Result getSeckillSkuIdStr(HttpServletRequest request,@PathVariable Long skuId){
        // auth/getSeckillSkuIdStr?skuId=xxx String skuId = request.getParameter("skuId");
        System.out.println("skuId: \t"+skuId);
        // 获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        // 什么时候才能让用户获取下单码？
        if (!StringUtils.isEmpty(userId)){
            // 活动开始之后，结束之前！
            SeckillGoods seckillGoods = seckillGoodsService.getSeckillGoods(skuId);
            // 获取当前系统时间
            Date curTime = new Date();
            // 应该获取下单码
            if (DateUtil.dateCompare(seckillGoods.getStartTime(),curTime) &&
                    DateUtil.dateCompare(curTime,seckillGoods.getEndTime())){
                // 使用MD5 工具类对userId 进行加密 生产下单码
                String skuIdStr = MD5.encrypt(userId);
                return Result.ok(skuIdStr);
            }
        }
        return Result.fail().message("获取下单码失败");
    }

    // url: this.api_name + '/auth/seckillOrder/{skuId} + '?skuIdStr=' + skuIdStr,
    @PostMapping("auth/seckillOrder/{skuId}")
    public Result seckillOrder(@PathVariable Long skuId,HttpServletRequest request){
        /*
        1.  验证下单码 利用md5 对userId 进行加密
        2.  验证状态码
        3.  将用户放入mq
         */
        // 获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        // 获取页面传递过来的下单码
        String skuIdStr = request.getParameter("skuIdStr");
        // 判断
        if (!MD5.encrypt(userId).equals(skuIdStr)){
            // 提示不合法
            return Result.build(null, ResultCodeEnum.SECKILL_ILLEGAL);
        }
        // 验证状态码 存在内存中！
        // skuId:1 | skuId:0
        String state = (String) CacheHelper.get(skuId.toString());
        // 判断
        if(StringUtils.isEmpty(state)){
            // 提示不合法
            return Result.build(null, ResultCodeEnum.SECKILL_ILLEGAL);
        }

        // 继续判断
        if ("1".equals(state)){
            // 声明对象UserRecode 
            UserRecode userRecode = new UserRecode();
            // 赋值：哪个用户购买哪个商品
            userRecode.setUserId(userId);
            userRecode.setSkuId(skuId);

            // 发送消息到mq 中，排队！
            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_SECKILL_USER,MqConst.ROUTING_SECKILL_USER,userRecode);
        }else {
            // 已售罄
            return Result.build(null, ResultCodeEnum.SECKILL_FINISH);
        }
        // 返回
        return Result.ok();
    }

    // 检查订单状态的控制器
    @GetMapping(value = "auth/checkOrder/{skuId}")
    public Result checkOrder(@PathVariable Long skuId,HttpServletRequest request){
        // 获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        Result result = seckillGoodsService.checkOrder(skuId, userId);
        // 返回数据
        return result;
    }

    // 封装下单数据接口
    @GetMapping("auth/trade")
    public Result trade(HttpServletRequest request){
        // 1.   获取用户Id
        String userId = AuthContextHolder.getUserId(request);

        // 获取到送货清单 ，秒杀从哪获取?
        OrderRecode orderRecode = (OrderRecode) redisTemplate.boundHashOps(RedisConst.SECKILL_ORDERS).get(userId);
        // 判断
        if (orderRecode==null){
            return Result.fail().message("非法操作");
        }
        // 获取用户收货地址列表
        List<UserAddress> userAddressList = userFeignClient.findUserAddressListByUserId(userId);

        // 获取用户秒杀的哪个商品
        SeckillGoods seckillGoods = orderRecode.getSeckillGoods();

        // 创建一个订单明细
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setSkuId(seckillGoods.getSkuId());
        orderDetail.setSkuName(seckillGoods.getSkuName());
        orderDetail.setImgUrl(seckillGoods.getSkuDefaultImg());
        orderDetail.setSkuNum(orderRecode.getNum());
        orderDetail.setOrderPrice(seckillGoods.getCostPrice());

        // 将orderDetail 放入一个集合
        ArrayList<OrderDetail> detailArrayList = new ArrayList<>();
        detailArrayList.add(orderDetail);

        // 计算总金额
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderDetailList(detailArrayList);
        orderInfo.sumTotalAmount();

        // 声明一个map 集合
        Map<String, Object> result = new HashMap<>();
        result.put("userAddressList",userAddressList);
        result.put("detailArrayList",detailArrayList);
        result.put("totalAmount",orderInfo.getTotalAmount());
        // result.put("totalNum",orderDetail.getSkuNum()); 可以写，也可以不写！
        return Result.ok(result);
    }

    // 保存秒杀订单
    @PostMapping("auth/submitOrder")
    public Result submitOrder(@RequestBody OrderInfo orderInfo,HttpServletRequest request){
        // 获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        orderInfo.setUserId(Long.parseLong(userId));

        // 严禁的判断：1.通过userId 来判断用户到底是否下过订单
        OrderRecode orderRecode = (OrderRecode) redisTemplate.boundHashOps(RedisConst.SECKILL_ORDERS).get(userId);
        if (orderRecode==null){
            // 提示信息
            return Result.fail().message("非法操作！");
        }

        // 调用订单的保存方法
        Long orderId = orderFeignClient.submitOrder(orderInfo);
        if (orderId ==null){
            return Result.fail().message("下单失败！");
        }

        // 根据userId删除预订单
        redisTemplate.boundHashOps(RedisConst.SECKILL_ORDERS).delete(userId);
        // 将秒杀的商品数据保存到缓存，目的是为了让用户只能购买一次！
        // key = seckill:orders:users field = userId value = orderId
        redisTemplate.boundHashOps(RedisConst.SECKILL_ORDERS_USERS).put(userId, orderId.toString());

        return Result.ok(orderId);
    }
}
